<template>
    <div class="addDialog" id="addDialog">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="类型名称" prop="typeName">
                <el-input v-model="ruleForm.typeName"></el-input>
            </el-form-item>
            <el-form-item label="类型标签" prop="typeTag">
                <el-input v-model="ruleForm.typeTag"></el-input>
            </el-form-item>
            <el-form-item label="图片">
                <el-upload
                    action="http://152.136.226.133:8080/hardcore/file/upload?fileType=image"
                    list-type="picture-card"
                    :file-list="fileList"
                    :on-success="imageUrlSuccess"
                    :limit="1"
                    :headers="myHeaders"
                >
                    <i class="el-icon-plus"></i>
                </el-upload>
            </el-form-item>
        </el-form>
        <div class="dialogFooter">
            <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
        </div>
    </div>
</template>
<script>
export default {
    props: {
        editOneData: {
            type: Object,
            default: {}
        }
    },
    watch: {
        editOneData: {
            handler(val) {
                this.ruleForm = val;
                this.fileList[0] = { name: '图片', url: val.imageUrl };
            },
            deep: true,
            immediate: true
        }
    },
    data() {
        return {
            myHeaders: { token: localStorage.getItem('token') },
            fileList: [],
            ruleForm: {
                typeName: '',
                typeTag: '',
                imageUrl: ''
            },
            rules: {
                typeName: [{ required: true, message: '请输入类型名称', trigger: 'blur' }],
                typeTag: [{ required: true, message: '请输入类型标签', trigger: 'blur' }],
                imageUrl: [{ required: true, message: '请输入图片', trigger: 'blur' }]
            }
        };
    },
    mounted() {},
    methods: {
        imageUrlSuccess(response) {
            this.ruleForm.imageUrl = response.data.url;
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.$emit('typeEdit', this.ruleForm);
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        }
    }
};
</script>
<style lang="scss">
#addDialog {
    .el-upload--text {
        border: none;
        height: 32px;
        width: 80px;
    }
}
</style>
<style scoped>
.el-upload--text {
    border: none;
}
.dialogFooter {
    display: flex;
    justify-content: flex-end;
}
</style>
